﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using SDSystem.Models;
using System.Linq.Dynamic;

namespace SDSystem.Controllers
{
    public class AddressController : Controller
    {

        //
        // GET: /Address/

        #region demo data
        private static Country sampleCountry = new Country { CountryId = 1, CountryName = "Britain" };
        private static County sampleCounty = new County { CountyId = 1, CountyName = "Greenwich", Country = sampleCountry };
        private static Town sampleTown = new Town { TownId = 1, Country = sampleCountry, County = sampleCounty, TownName = "London" };

        private static List<Address> SampleData = new List<Address>
                                               {
                                                   new Address{AddressId = 1,PostCode = "SE10 OSS",Town = sampleTown, County = sampleCounty,Country = sampleCountry,Street = "10 Maurer Court"},
                                                   new Address{AddressId = 2,PostCode = "SE11 OSS",Town = sampleTown, County = sampleCounty,Country = sampleCountry,Street = "10 Maurer Court"},
                                                   new Address{AddressId = 3,PostCode = "SE12 OSS",Town = sampleTown, County = sampleCounty,Country = sampleCountry,Street = "10 Maurer Court"},
                                                   new Address{AddressId = 4,PostCode = "SE13 OSS",Town = sampleTown, County = sampleCounty,Country = sampleCountry,Street = "10 Maurer Court"},
                                                   new Address{AddressId = 5,PostCode = "SE14 OSS",Town = sampleTown, County = sampleCounty,Country = sampleCountry,Street = "10 Maurer Court"},
                                                   new Address{AddressId = 6,PostCode = "SE15 OSS",Town = sampleTown, County = sampleCounty,Country = sampleCountry,Street = "10 Maurer Court"}
                                               };
        #endregion

        private SDSystemEntities db = new SDSystemEntities();

        [Authorize(Roles = "users")]
        public ActionResult Lookup()
        {
            return View();
        }

        // Search
        [Authorize(Roles = "users")]
        public ActionResult Search()
        {
            var store = X.GetCmp<Store>("AddressStore");
            // search here

            Session["sPostCode"] = X.GetCmp<TextField>("txtPostCode").Text;
            Session["sStreet"] = X.GetCmp<TextField>("txtStreet").Text;
            Session["sTown"] = X.GetCmp<TextField>("txtTown").Text;

            //X.Msg.Alert("Name", Session["sStreet"] + " - " + Session["sTown"]).Show();

            store.Reload();
            return this.Direct();
        }

        [Authorize(Roles = "users")]
        public ActionResult ClearSearch()
        {
            var store = X.GetCmp<Store>("AddressStore");
            // Clear search here
            Session["sPostCode"] = X.GetCmp<TextField>("txtPostCode").Text = "";
            Session["sStreet"] = X.GetCmp<TextField>("txtStreet").Text = "";
            Session["sTown"] = X.GetCmp<TextField>("txtTown").Text = "";

            store.Reload();
            return this.Direct();
        }

        [Authorize(Roles = "users")]
        public ActionResult BindingStore(StoreRequestParameters parameters)
        {
            // Search
            string postcode = (string)Session["sPostCode"];
            string street = (string)Session["sStreet"];
            string town = (string)Session["sTown"];

            var result = db.Addresses.Where(ent =>
                   (string.IsNullOrEmpty(postcode) || ent.PostCode.Contains(postcode)) &&
                   (string.IsNullOrEmpty(street) || ent.Street.Contains(street)) &&
                   (string.IsNullOrEmpty(town) || ent.Town.TownName.Contains(town))
                );

            // Sorting
            if (!string.IsNullOrEmpty(parameters.SimpleSort))
            {
                result = result.OrderBy(parameters.SimpleSort + (parameters.SimpleSortDirection == SortDirection.DESC ? " desc" : " asc"));
            }
            else
            {
                result = result.OrderByDescending(ent => ent.AddressId);
            }

            // Paging
            int limit = parameters.Limit;
            if ((parameters.Start + parameters.Limit) > result.Count())
            {
                limit = result.Count() - parameters.Start;
            }

            var rangeData = (parameters.Start < 0 || limit < 0) ? result : result.Skip(parameters.Start).Take(limit);
            return this.Store(rangeData.Select(ent => new { ent.AddressId, ent.Street, ent.PostCode, Town = ent.Town.TownName, County = ent.County.CountyName, Country = ent.Country.CountryName }), result.Count());
        }


    }
}
